package com.sw.core.doc.doc.docimpl;

import java.io.FileOutputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.sw.core.doc.doc.DocBaseOpr;
import com.sw.core.doc.doc.DocUtil;
import com.sw.core.exception.DocOprException;
import com.sw.core.expopr.WKAppException;
import com.tapestry.app.core.data.IList;
import com.tapestry.app.core.data.IMap;

public class ExcelUtil implements DocUtil {

	public void createFile(String fileName, String fileType, IList data) {
		createFile("",fileName,fileType,data);
	}
	
	
	public void createFile(String fileName, String fileType) {
		createFile("",fileName,fileType,null);
	}

	public void createFile(String filePath, String fileName, String fileType,IList data) {
		try{
			//****生成97-03格式的excel文档
			if("xls".equals(fileType)){
				this.createDataExcel(filePath,fileName,data);
			}else if("xlsx".equals(fileType)){ //****生成07格式的excel文档
				this.createExcel(filePath,fileName,data);
			}
		}catch(Exception e){
			
		}
		
	}

	public IList getFileInfo(String file) {
		
		return null;
	}

	@Override
	public IList modifyFile(String file, IList modifyData) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean delFile(String file) {
		// TODO Auto-generated method stub
		return false;
	}
	
	/**
	 * @Title : createDataExcel
	 * @Description: 生成格式为xls的excel
	 * @Param : @param file
	 * @Param : @param list
	 * @Param : @throws IOException
	 * @return: void
	 * @throws Exception 
	 */
	private void createDataExcel(String path,String fileName,IList list) throws Exception{
		if(list == null || list.size() <= 0){
			WKAppException.apperr(DocOprException.DOC_EXCEPTION_5);
		}
		
		HSSFWorkbook workbook = new HSSFWorkbook();
		HSSFSheet sheet = workbook.createSheet();
		for(int i = 0 ; i < list.size() ; i++){
			IMap data = list.getData(i);
			String[] keys = data.getNames();
			HSSFRow rowhead = null;
			HSSFRow row = sheet.createRow(i+1);
			for(int j = 0 ; j < data.size() ; j++){
				if(i == 0){
					if(j == 0){
						rowhead = sheet.createRow(0);
					}
					HSSFCell cellhead = rowhead.createCell(j);
					cellhead.setCellValue(keys[j]);
				}
				HSSFCell cell = row.createCell(j);
				cell.setCellValue(data.getString(keys[j]));
			}
		}
		String file = DocBaseOpr.coventFileStr(path,fileName,"xls");
		FileOutputStream fos = new FileOutputStream(file);
		workbook.write(fos);
		fos.close();
	}
	
	/**
	 * @Title : createDataExcel
	 * @Description: 生成格式为xls的excel
	 * @Param : @param file
	 * @Param : @param list
	 * @Param : @throws IOException
	 * @return: void
	 * @throws Exception 
	 */
	private void createExcel(String path,String fileName,IList list){
		
		if(list == null || list.size() <= 0){
			WKAppException.apperr(DocOprException.DOC_EXCEPTION_5);
		}
		
		XSSFWorkbook workbook = new XSSFWorkbook();
		
		
	}

}
